﻿using System;
using System.Linq;
using System.Data;

namespace ALDIOrganizer
{    
    public class Employee : IComparable
    {
        internal DataRow row;
        public Employee(DataRow row)
        {
            this.row = row;
        }

        public int hours;
        public Position role;

        public Int64 ID
        {
            get { return (Int64)row[0];  }
            set { row[0] = (Int64)value; }
        }

        public string firstName
        {
            get { return (string)row[1]; }
            set { row[1] = (string)value; }
        }

        public string lastName
        {
            get { return (string)row[2]; }
            set { row[2] = (string)value; }
        }

        public string phone
        {
            get { return (string)row[3]; }
            set { row[3] = (string)value; }
        }

        public string mobile
        {
            get { return (string)row[4]; }
            set { row[4] = (string)value; }
        }

        public DateTime birthDate
        {
            get { return (DateTime)row[5]; }
            set { row[5] = (DateTime)value; }
        }

        public bool sex
        {
            get { return (bool)row[6]; }
            set { row[6] = (bool)value; }
        }

        public string address
        {
            get { return (string)row[7]; }
            set { row[7] = (string)value; }
        }

        public string NIN
        {
            get { return (string)row[8]; }
            set { row[8] = (string)value; }
        }

        public string currentStatus
        {
            get { return (string)row[9]; }
            set { row[9] = (string)value; }
        }

        public bool currentEmployedNow
        {
            get { return (bool)row[10]; }
            set { row[10] = (bool)value ? 1 : 0; }
        }

        public bool currentOffProd
        {
            get { return (bool)row[11]; }
            set { row[11] = (bool)value ? 1 : 0; }
        }

        public Int64 currentContractHRS
        {
            get { return (Int64)row[12]; }
            set { row[12] = (Int64)value; }
        }

        public Int64 currentHolidayAllowance
        {
            get { return (Int64)row[13]; }
            set { row[13] = (Int64)value; }
        }

        public Position currentPosition
        {
            get
            {
                if (row[14].GetType() == typeof(Int64))
                    return ((Int64)row[14]).To<Position>();
                return ((long)0).To<Position>();
            }
            set { row[14] = (Int64)value; }
        }

        public string displayColumn
        {
            get { return (string)row[15]; }
            set { row[15] = (string)value; }
        }

        public bool OnVisa
        {
            get
            {
                if (row.ItemArray.Length < 17 ||
                    row[16] == null ||
                    row[16] == DBNull.Value)                    
                {
                    return false;
                }                
                return (bool)row[16];
            }
            set
            {
                row[16] = (bool)value ? 1 : 0;
            }
        }

        public string FullDescription()
        {
            var name = "";

            name += this.currentPosition.ToShortString();
            name += "\t";
            name += SQLiteAccess.GetEmployeeFullName(ID);
                        
            return name;
        }

        public void TakeContract(Contract c)
        {
            currentEmployedNow = c.Hired;
            currentOffProd = c.role.IsOffProd();
            currentPosition = c.role;
            currentContractHRS = c.contractHrs;
            currentHolidayAllowance = c.holidayAllowance;
        }

        public static Employee[] ToEmployees(DataRow[] rows)
        {
            return rows.Select(row => row.AsEmployee()).ToArray();
        }

        public void AcceptChanges()
        {
            row.AcceptChanges();
        }

        




        #region IComparable Members

        /// <summary>
        /// Compares two instances of the Employee class
        /// </summary>
        /// <param Name="obj"></param>
        /// <remarks>
        /// It'pShift used for sorting the emps_from_database On the datagridview.
        /// The hierarchy of rules is as follows:
        /// 1. people who are On-productivity first. off-productivity emps_from_database (trainees) - last.
        /// 2. higher ranked emps_from_database first. (eg. store manager before an assistant manager).
        /// 3. if the job Position is the same, sort alphabetically by last Name.
        /// 4. if last names are the same, sort by first Name.
        /// </remarks>
        public int CompareTo(object obj)
        {
            if (!(obj is Employee)) return 0;
            var e2 = obj as Employee;
                        
            if (this.role == e2.role)
            {
                if (this.lastName() == e2.lastName())
                    return this.FirstName().CompareTo(e2.FirstName());
                else
                    return this.lastName().CompareTo(e2.lastName());
            }
            else
            {
                // ^ means XOR in C#.
                // basically, we're verifying whether their off-productivity status is the same or not.
                if (this.role.IsOffProd() ^ e2.role.IsOffProd())
                    return this.role.IsOffProd() ? 1 : -1;
                    // if it'pShift different, then the one who'pShift off productivity is "bigger" (lower On the list)
                else
                {
                    var r1 = (int)this.role.GetAsOnProd();
                    var r2 = (int)e2.role.GetAsOnProd();
                    return r2.CompareTo(r1); // reversed, because store manager (32) should be smaller than assistant manager (16)                    
                }                
            }            
            // REMEMBER: sorting will be ascending, so "smaller" means "goes first", "higher On the list" etc.!!!
        }

        #endregion
    }

    
}
